package com.zcn.io.web.excel;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.zcn.io.common.entity.OrderMain;
import com.zcn.io.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/2 19:14
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
public class OrderListener extends BaseListener{

    private OrderService service;

    public OrderListener() {
        super((item)->{});
    }

    public OrderListener(OrderService orderService) {
        super((item)->{});
        service = orderService;
    }

    public OrderListener(Consumer consumer) {
        super(consumer);
    }

    public OrderListener(Consumer consumer, int batchCount) {
        super(consumer, batchCount);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        List<Map<Integer, String>> cacheDataList = this.getCacheDataList();
        List<OrderMain> saveDataList = cacheDataList.stream().map(item -> {
            OrderMain orderMain = new OrderMain();
            orderMain.setIndexId(StrUtil.toString(item.get(1)));
            orderMain.setTrusteeshipType(StrUtil.toString(item.get(3)));
            orderMain.setTransferPhone(StrUtil.toString(item.get(4)));
            orderMain.setCustomName(StrUtil.toString(item.get(5)));
            orderMain.setSex(StrUtil.toString(item.get(6)));
            if (item.get(7)!=null) {
                DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy.M.d");
                LocalDate date = LocalDate.parse("20" + item.get(7), inputFormatter);
                DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                orderMain.setOrderReceiveDate(date.format(outputFormatter)+ " 00:00:00");
            }

            if (item.get(8)!=null) {
                DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy.M.d");
                LocalDate date = LocalDate.parse("20" + item.get(8), inputFormatter);
                DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                orderMain.setOrderEndDate(date.format(outputFormatter)+ " 00:00:00");
            }
            orderMain.setOrderStatus("处理中");
            orderMain.setTrusteeshipType("托管");
            return orderMain;
        }).collect(Collectors.toList());
        service.saveBatch(saveDataList);
    }
}
